package com.wy.util;


import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class TxtUtils {
    public static void main(String[] args) {
       // System.out.println(getFileContent("D:\\2021\\ssm\\123.txt"));
        //System.out.println(getAllTxt("D:\\2021\\ssm\\123.txt"));
      /*  String path = TxtUtils.class.getClassLoader().getResource("./123.txt").getPath();
        System.out.println(path);*/
        System.out.println(getResourcesFileContent("123.txt"));
    }
    //读取resources下文件
    public static List<String> getResourcesFileContent(String path) {
        InputStream inputStream = TxtUtils.class.getClassLoader().getResourceAsStream("./"+path);
        return getFileContent(inputStream);
    }
    public static List<String> getFileContent(String path) {
        File file = new File(path);
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return getFileContent(fileInputStream);
    }
    public static List<String> getFileContent(InputStream inputStream) {
        List strList = new ArrayList();
        InputStreamReader inputStreamReader=null;
        BufferedReader bufferedReader = null;
        try {
            inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null)
                strList.add(line);
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return strList;
    }

    public static String getAllTxt(String filePath) {
        File file = new File(filePath);
        Long fileLengthLong = Long.valueOf(file.length());
        byte[] fileContent = new byte[fileLengthLong.intValue()];
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(fileContent);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new String(fileContent);
    }

}